package com.sc.study.student.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.sc.study.student.domain.ScStudent;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.student.domain.ScStudentClazz;
import com.sc.study.student.service.IScStudentClazzService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;

/**
 * 学员班级Controller
 *
 * @author sc
 * @date 2022-02-27
 */
@RestController
@RequestMapping("/study/studentclazz")
public class ScStudentClazzController extends BaseController
{
    @Autowired
    private IScStudentClazzService scStudentClazzService;


    /**
     * 查询学员班级列表包含班级，学员，年级
     */
    @PreAuthorize("@ss.hasPermi('study:studentclazz:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScStudentClazz scStudentClazz)
    {
        startPage();
        List<ScStudentClazz> list = scStudentClazzService.selectScStudentClazzList(scStudentClazz);
        return getDataTable(list);
    }

    /**
     * 导出学员班级列表
     */
    @PreAuthorize("@ss.hasPermi('study:studentclazz:export')")
    @Log(title = "学员班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScStudentClazz scStudentClazz)
    {
        List<ScStudentClazz> list = scStudentClazzService.selectScStudentClazzList(scStudentClazz);
        ExcelUtil<ScStudentClazz> util = new ExcelUtil<ScStudentClazz>(ScStudentClazz.class);
        util.exportExcel(response, list, "学员班级数据");
    }

    /**
     * 获取学员班级详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:studentclazz:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(scStudentClazzService.selectScStudentClazzById(id));
    }

    /**
     * 新增学员班级
     */
    @PreAuthorize("@ss.hasPermi('study:studentclazz:add')")
    @Log(title = "学员班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Map<String,Object> map)
    {
        //拿到班级Id
        Long studentClazzId = Long.valueOf(map.get("studentClazzId").toString());
        //拿到学生集合
        List<ScStudent> scStudentList= JSON.parseArray(map.get("scStudentList").toString(),ScStudent.class);

        return toAjax(scStudentClazzService.insertScStudentClazz(scStudentList,studentClazzId));

    }

    /**
     * 修改学员班级
     */
    @PreAuthorize("@ss.hasPermi('study:studentclazz:edit')")
    @Log(title = "学员班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScStudentClazz scStudentClazz)
    {
        return toAjax(scStudentClazzService.updateScStudentClazz(scStudentClazz));
    }

    /**
     * 删除学员班级
     */
    @PreAuthorize("@ss.hasPermi('study:studentclazz:remove')")
    @Log(title = "学员班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scStudentClazzService.deleteScStudentClazzByIds(ids));
    }

    /**
     * 学员班级查看
     * 查询学员班级 包含班级、学员、年级 并且查看这个学员的历史班级
     * @param id 学员班级主键
     * @return 学员班级
     */
    @PreAuthorize("@ss.hasPermi('study:studentclazz:look')")
    @Log(title = "学员班级", businessType = BusinessType.DELETE)
    @GetMapping("/look/{id}")
    public AjaxResult look(@PathVariable("id") Long id){
        return AjaxResult.success(scStudentClazzService.selectScStudentClazzIncludeHistoryClazz(id));
    }
}
